<?php

namespace admin\models;

use ijony\helpers\File;
use ijony\helpers\Image;
use Yii;

/**
 * This is the model class for table "{{%case_galleries}}".
 *
 * @property string $case_id 案例 ID
 * @property string $file 图片
 */
class CaseGalleries extends \common\models\CaseGalleries
{

    public function beforeSave($insert)
    {
        if($this->file && substr($this->file, 0, 6) == BUFFER_FOLDER){
            $oldImg = $this->file;
            $newImg = Image::copyImg($this->file);

            if($newImg){
                File::delFile($oldImg, true);
            }

            $this->file = $newImg;
        }

        return parent::beforeSave($insert);
    }

    public function afterDelete()
    {
        parent::afterDelete();

        File::delFile($this->file, true);
    }
}
